<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>又拍云 - js sdk 使用示例</title>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <script src="../dist/upyun.js"></script>
</head>
<body>
  <input type="file" id="file" name="file">
  <input type="submit" value="upload" onclick="upload()">
  <script>
    // client side only need bucket name
    var bucket = new upyun.Bucket('sdkimg')
    function getHeaderSign(bucket, method, path) {
      var params = 'bucket=' + bucket.bucketName + '&method=' + method + '&path=' + path
      return fetch('/sign/head?' + params)
        .then(function(response) {
          if (response.status !== 200) {
            console.error('gen header sign faild!')
            return;
          }
          return response.json()
        })
    }
    var client = new upyun.Client(bucket, getHeaderSign)

    function upload() {
      var file = document.getElementById('file').files[0]
      client.putFile('/sample-upload-' + file.name, file).then(function(result) {
        console.log('put file to upyun ' + (result ? 'success' : 'failed'))
      })
    }
  </script>
</body>
</html>
